<?php
  class UserGroup  
  {
	public $db;
	
	public function __construct($db)
	{
		if(is_object($db))
		{
			$this->db = $db;
		}
		else
		{
			die('User Class __construct Need $db object (d3sql Class).');
		}
	}
	
	/**
	* get Group Details
	* 
	* @param mixed $group_id
	*/
	public function getGroup($group_id = false)
	{
		$groupSQLText = " SELECT g.*,(SELECT count(*) as total FROM snm_users WHERE authority_group_id = g.id) as count_user FROM snm_group g ";
		if($group_id)
		{
			if($group_id > 0)
			{
				$groupSQLText .= " WHERE g.id = ".intval($group_id);
			}
			else
			{
				die('Group ID is not > 0');
			}
		}
		
		$groupSQLText .= " ORDER BY g.group_name ASC ";
		
		$groupSQL = $this->db->sql($groupSQLText);       
		$groups = array();
		while($group = $this->db->getir($groupSQL))
		{
			$groups[] = $group;
		}
		
		return $groups;
	}
	
	/**
	* Return All Authority Group
	* 
	* @param mixed $groupID
	*/
	public function getAllAuthority($groupID)
	{
		$AuthListSQL = $this->db->sql(" SELECT * FROM snm_authority au WHERE au.group_id = ".intval($groupID));
		$AuthListArray = array();
		while($AuthList = $this->db->getir($AuthListSQL) )
		{
			$AuthListArray[] = $AuthList;
		}
		return $AuthListArray;
	}
	
	/**
	* Get Authority by Module and userID
	* 
	* @param mixed $module
	* @param mixed $userID
	*/
	public function getAuthority($module,$userID)
	{
		$authoritySQLText = "SELECT count(*) as yetki FROM snm_users u LEFT JOIN snm_group g ON u.authority_group_id = g.id LEFT JOIN snm_authority a ON a.group_id = g.id WHERE u.id =".$userID." AND a.module = '".$module."'";
		$authoritySQL = $this->db->sql($authoritySQLText);
		$authority = $this->db->getir($authoritySQL);
		if($authority['yetki'] > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
  }
?>
